
/* --------------------------------------
 * Copyright 2008 Jean-Matthieu BARBIER
 * --------------------------------------
 * This file is part of QLocatePC.
 *
 * QLocatePC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QLocatePC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QLocatePC.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QApplication>
#include <QtGui>
#include <QTimer>
#include "setup.h"
#include "projectinfo.h"
#include "setupwin.h"
#include "httplocate.h"

int main(int argc, char ** argv)
{
  QCoreApplication::setOrganizationName(QLPC_ORGNAME);
  QCoreApplication::setOrganizationDomain(QLPC_ORGDOMAIN);
  QCoreApplication::setApplicationName(QLPC_APPNAME);
  QCoreApplication::setApplicationVersion(QLPC_APPVERSION);
  QApplication          app( argc, argv );
  QLocatePCSetupData    setup;
  QLocatePCHttpLocate   loc(&app);
  QLocatePCSetupWin     win;
  setup.load();
  QStringList           args = app.arguments();
  if((args.indexOf("--setup")!=-1) | (args.indexOf("-s")!=-1)) {
      //- Run UI setup
      win.updateFromSetup(&setup);
      win.show();
  } else if((args.indexOf("--daemon")!=-1) | (args.indexOf("-d")!=-1)) {
      //- Daemonize LocatePC - run daemonized
      setup.daemon = true;
      setup.set(); loc.updateDaemon();
      QObject::connect(&loc,SIGNAL(daemonKilled()), &app, SLOT(quit()));
  } else if((args.indexOf("--kill")!=-1) | (args.indexOf("-k")!=-1)) {
      //- Destroys existing daemons and quit
      setup.daemon = false;
      setup.set();
      return 0;
  } else if((args.indexOf("--help")!=-1) | (args.indexOf("-h")!=-1)) {
    //- Print help
  } else if(setup.daemon) {
    //- Run silently in daemon mode
    QObject::connect(&loc,SIGNAL(daemonKilled()), &app, SLOT(quit()));
    loc.updateDaemon();
  } else {
    //- Run silently one time
    QObject::connect(&loc,SIGNAL(locFailed(QString)),&app,SLOT(quit()));
    QObject::connect(&loc,SIGNAL(locSuccess(QString)),&app,SLOT(quit()));
    loc.goLocate(&setup, QLocatePCHttpLocate::actionLocate);
  }
  return app.exec();
}

